﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using EnterpriseFramework.Common;
using System.Xml.Serialization;
using System.IO;

namespace EnterpriseFramework.Configuration
{
    [Serializable]
    public class ConfigurationFactory
    {
        XmlDocument config = null;
        public ConfigurationFactory()
        {
            config = new XmlDocument();

            config.Load("Config.config");
        }

        public BasicSetting GetBasicSetting()
        {
            XmlNode nodeBasicSet = config.SelectNodes("//" + "BasicSetting")[0];
            BasicSetting bs = new BasicSetting();
            bs.Language = nodeBasicSet["Language"].InnerText;

            if (!string.IsNullOrEmpty(nodeBasicSet["MaximumCache"].InnerText))
                bs.MaximumCache = Convert.ToInt32(nodeBasicSet["MaximumCache"].InnerText);
            else
                bs.MaximumCache = 5;

            if (!string.IsNullOrEmpty(nodeBasicSet["InternalDataBase"].InnerText))
                bs.ConnectionString = "Data Source=" + nodeBasicSet["InternalDataBase"]["Path"].InnerText +
                    ";Password=" + Convert.ToInt32(nodeBasicSet["InternalDataBase"]["Password"].InnerText) + ";";
            else
                bs.ConnectionString = string.Empty;
            return bs;
        }

        public LogConfig GetLogConfig()
        {
            XmlNode nodeBasicSet = config.SelectNodes("//" + "BasicSetting")[0];
            LogConfig log = new LogConfig();
            if (!string.IsNullOrEmpty(nodeBasicSet["Log"]["LogLevel"].InnerText))
            {
                LogLevel[] loglevel =
                    new LogLevel[nodeBasicSet["Log"]["LogLevel"].ChildNodes.Count];
                for (int i = 0; i < nodeBasicSet["Log"]["LogLevel"].ChildNodes.Count; i++)
                    loglevel[i] = (LogLevel)Enum.Parse
                        (typeof(LogLevel), nodeBasicSet["Log"]["LogLevel"].ChildNodes[i].InnerText);
                log.logLevel = loglevel;
            }
            else
                log.logLevel[0] = LogLevel.All;

            if (!string.IsNullOrEmpty(nodeBasicSet["Log"]["MaxLog"].InnerText))
                log.MaxLog = Convert.ToInt32(nodeBasicSet["Log"]["MaxLog"].InnerText);
            else
                log.MaxLog = 50;

            if (!string.IsNullOrEmpty(nodeBasicSet["Log"]["LogHandledMessage"].InnerText))
                log.LogHandledMessage = Convert.ToBoolean(nodeBasicSet["Log"]["LogHandledMessage"].InnerText);
            else
                log.LogHandledMessage = false;

            log.ScheduleReport = false;
            if (!string.IsNullOrEmpty(nodeBasicSet["Log"]["ScheduleReport"].InnerText))
                if (nodeBasicSet["Log"]["ScheduleReport"].InnerText == "on")
                    log.ScheduleReport = true;

            return log;
        }

        public Providers GetProviders()
        {
            XmlNodeList nodes = config.SelectNodes("//" + "Providers")[0].SelectNodes("//" + "Provider");
            Provider[] providerinfos = new Provider[nodes.Count];
            Providers providers = new Providers();
            for (int i = 0; i < nodes.Count; i++)
            {
                providerinfos[i].ConnectionString = nodes[i]["ConnectionString"].InnerText;
                if (!string.IsNullOrEmpty(nodes[i]["ExternalAssembly"].InnerText))
                    providerinfos[i].ExternalAssembly = nodes[i]["ExternalAssembly"].InnerText;
                providerinfos[i].Name = nodes[i]["Name"].InnerText;
                providerinfos[i].ProviderClass = nodes[i]["ProviderClass"].InnerText;
            }

            providers.Default = config.SelectNodes("//" + "Providers")[0]["Default"].InnerText;
            providers.ProviderList = providerinfos;

            return providers;
        }          

        private object Factory(XmlNode nodeBasicSet, Type type)
        {
            XmlSerializer resultBasicSet = new XmlSerializer(type);
            StringReader sr = new StringReader(nodeBasicSet.OuterXml);
            return resultBasicSet.Deserialize(sr);
        }
    }
}
